package com.bz.sunmipay.controller;

import android.content.Context;
import android.text.TextUtils;

import com.baizhi.baseapp.util.PreferencesUtil;
import com.bz.sunmipay.model.UserInfo;

/**
 * 共有数据封装(1，功课记录数据,2，用户个人信息数据)
 * <p/>
 * Created by Administrator on 2015/5/13.
 */
public class GlobalDataHolder {

    private static GlobalDataHolder instance;

    private static final String KEY_NICKNAME = "nickname";
    private static final String KEY_GENDER = "gender";
    private static final String KEY_DESCRIPTION = "description";
    private static final String KEY_LOCATION = "location";
    private static final String KEY_PHOTO = "photo";
    private static final String KEY_UID = "uid";
    private static final String KEY_PHONE = "phone";
    private static final String KEY_PASSWORD = "password";
    private static final String KEY_PROVINCE = "province";
    private static final String KEY_CITY = "city";

    private String uid;
    private UserInfo mUserInfo;
    private Context mContext;

    private GlobalDataHolder(Context context) {
        mContext = context.getApplicationContext();
    }

    public synchronized static GlobalDataHolder getInstance(Context context) {
        if (instance == null) {
            instance = new GlobalDataHolder(context);
        }
        return instance;
    }

    public String getUid() {
        return uid;
    }

    /**
     * 设置用户信息
     *
     * @param userInfo
     */
    public void setUserInfo(UserInfo userInfo) {
        this.mUserInfo = userInfo;
        if (userInfo == null) {
            PreferencesUtil.setStringByName(mContext, KEY_PASSWORD, "");
            PreferencesUtil.setStringByName(mContext, KEY_NICKNAME, "");
            PreferencesUtil.setStringByName(mContext, KEY_GENDER, "m");
            PreferencesUtil.setStringByName(mContext, KEY_DESCRIPTION, "");
            PreferencesUtil.setStringByName(mContext, KEY_LOCATION, "");
            PreferencesUtil.setStringByName(mContext, KEY_PHOTO, "");
            PreferencesUtil.setStringByName(mContext, KEY_UID, "");
            PreferencesUtil.setStringByName(mContext, KEY_PHONE, "");
            PreferencesUtil.setStringByName(mContext, KEY_PROVINCE, "");
            PreferencesUtil.setStringByName(mContext, KEY_CITY, "");
            this.uid = null;
        } else {
            PreferencesUtil.setStringByName(mContext, KEY_PASSWORD, userInfo.getPassword());
            PreferencesUtil.setStringByName(mContext, KEY_NICKNAME, userInfo.getNickname());
            PreferencesUtil.setStringByName(mContext, KEY_GENDER, userInfo.getGender());
            PreferencesUtil.setStringByName(mContext, KEY_DESCRIPTION, userInfo.getDescription());
            PreferencesUtil.setStringByName(mContext, KEY_LOCATION, userInfo.getLocation());
            PreferencesUtil.setStringByName(mContext, KEY_PHOTO, userInfo.getPhoto());
            PreferencesUtil.setStringByName(mContext, KEY_UID, userInfo.getUid());
            PreferencesUtil.setStringByName(mContext, KEY_PHONE, userInfo.getPhone());
            PreferencesUtil.setStringByName(mContext, KEY_PROVINCE, userInfo.getProvince());
            PreferencesUtil.setStringByName(mContext, KEY_CITY, userInfo.getCity());
            this.uid = userInfo.getUid();
        }
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public UserInfo getUserInfo() {
        //如果用户信息已经存在，直接返回
        if (mUserInfo != null) {
            return mUserInfo;
        }
        //没有登录的话直接返回空
        if (!isLogin()) {
            mUserInfo = null;
            return null;
        }
        //已经登录过，则初始化用户信息
        final String nickName = PreferencesUtil.getStringByName(mContext, KEY_NICKNAME, "");
        final String gender = PreferencesUtil.getStringByName(mContext, KEY_GENDER, "m");
        final String description = PreferencesUtil.getStringByName(mContext, KEY_DESCRIPTION, "");
        final String location = PreferencesUtil.getStringByName(mContext, KEY_LOCATION, "");
        final String phone = PreferencesUtil.getStringByName(mContext, KEY_PHONE, "");
        final String photo = PreferencesUtil.getStringByName(mContext, KEY_PHOTO, "");
        final String uid = PreferencesUtil.getStringByName(mContext, KEY_UID, "");
        final String password = PreferencesUtil.getStringByName(mContext, KEY_PASSWORD, "");
        final String province = PreferencesUtil.getStringByName(mContext, KEY_PROVINCE, "");
        final String city = PreferencesUtil.getStringByName(mContext, KEY_CITY, "");

        final UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickName);
        userInfo.setGender(gender);
        userInfo.setDescription(description);
        userInfo.setLocation(location);
        userInfo.setPhoto(photo);
        userInfo.setUid(uid);
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setProvince(province);
        userInfo.setCity(city);
        this.mUserInfo = userInfo;
        this.uid = uid;

        return mUserInfo;
    }

    /**
     * 是否登录
     *
     * @return
     */
    public boolean isLogin() {
        return !TextUtils.isEmpty(PreferencesUtil.getStringByName(mContext, KEY_UID, ""));
    }

    public void logout() {
        PreferencesUtil.clearValues(mContext);
        mUserInfo = null;
    }
}
